package com.ruoyi.dynamic.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.pojo.result.PageUtils;
import com.ruoyi.common.core.pojo.result.Pages;
import com.ruoyi.common.core.utils.CollectionUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.dynamic.mapper.FamilyMapper;
import com.ruoyi.dynamic.model.constant.DynamicConstant;
import com.ruoyi.dynamic.model.pojo.entity.Family;
import com.ruoyi.dynamic.model.pojo.request.app.AppFamilySearchRequest;
import com.ruoyi.dynamic.model.pojo.result.app.AppFamilyResult;
import com.ruoyi.dynamic.service.IFamilyService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 家族 服务实现类
 * </p>
 *
 * @author Gnc
 * @since 2024-12-18
 */
@Service
public class IFamilyServiceImpl extends ServiceImpl<FamilyMapper, Family> implements IFamilyService {

    @Override
    public Pages<AppFamilyResult> listPage(AppFamilySearchRequest request, Long userId) {
        Page<Family> familyPage = PageUtils.initPage(request, Family.class);

        Page<Family> page = lambdaQuery()
                .and(StringUtils.hasText(request.getSearch()),
                        wapper -> wapper.like(Family::getName, request.getSearch())
                                .or()
                                .like(Family::getContent, request.getSearch())

                ).orderByDesc(Family::getCreateTime)
                .page(familyPage);

        List<AppFamilyResult> collect = page.getRecords().stream().map(res -> {
            return info(res);
        }).collect(Collectors.toList());

        return PageUtils.pages(familyPage, collect);
    }

    @Override
    public AppFamilyResult info(Family one) {
        return AppFamilyResult.builder()
                .backgroundUrl(one.getBackgroundUrl())
                .city(one.getCity())
                .content(one.getContent())
                .empiricalValue(one.getEmpiricalValue())
                .familyNum(one.getFamilyNum())
                .foreignGuestNum(one.getForeignGuestNum())
                .id(one.getId())
                .identity(one.getIdentity())
                .name(one.getName())
                .notice(one.getNotice())
                .province(one.getProvince())
                .region(one.getRegion())
                .status(one.getStatus())
                .build()
                ;
    }

    @Override
    public Family verify(Long id) {
        return null;
    }

    @Override
    public AppFamilyResult appInfo(Long id) {
        Family byId = getById(id);
        Assert.notNull(byId,"此家族不存在");
        Assert.isTrue(byId.getStatus().equals(DynamicConstant.CheckStatus.YES),"此家族已关闭");
        return info(byId);
    }
}
